import axios from "axios";
import { store } from "../store";

var service = axios.create({
  timeout: 100 * 1000,
  baseURL: "/api",
});

service.interceptors.request.use(
  (config) => {
    config.headers.token = store.getState().loginInfo.token;
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
service.interceptors.response.use(
  (res) => {
    return res;
  },
  (error) => {
    console.log(error);
    var status = error.response.status;
    if (status == 401) {
      alert("登录过期，请重新登录");
    } else if (status == 404) {
      alert("访问路径有误");
    } else if (status == 500) {
      alert("服务器内部错误");
    } else if (status == 503) {
      alert("服务器不可用");
    }
    Promise.reject(error);
  }
);
export default service;
